package com.soso.base.util.other;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ConversionUtil {
	
	private static final Map<String,String> REGEX_MAP;
	private static final Map<String,String> HTML_MAP;
	static{
		REGEX_MAP=new HashMap<String,String>();
		REGEX_MAP.put("\\+", "\\\\+");
		REGEX_MAP.put("\\*","\\\\*");
		REGEX_MAP.put("\\?","\\\\?");
		REGEX_MAP.put("\\.","\\\\.");
		REGEX_MAP.put("\\^","\\\\^");
		REGEX_MAP.put("\\[","\\\\[");
		REGEX_MAP.put("\\]","\\\\]");
		REGEX_MAP.put("\\(","\\\\(");
		REGEX_MAP.put("\\)","\\\\)");
		REGEX_MAP.put("\\{","\\\\{");
		REGEX_MAP.put("\\}","\\\\}");
		REGEX_MAP.put("\\$","\\\\$");
		REGEX_MAP.put("\\<","\\\\<");
		REGEX_MAP.put("\\>","\\\\>");
		
		HTML_MAP=new HashMap<String,String>();
		HTML_MAP.put("&quot;", "”");
		HTML_MAP.put("&bdquo;", "„");
		HTML_MAP.put("&quot;", "\"");
		HTML_MAP.put("&frasl;", "/");
		HTML_MAP.put("&lt;", "<");
		HTML_MAP.put("&gt;", ">");
		HTML_MAP.put("&ndash;", "–");
		HTML_MAP.put("&mdash;", "—");
		HTML_MAP.put("&copy;", "©");
		HTML_MAP.put("&trade;", "™");
		HTML_MAP.put("&nbsp;", " ");
	}
	/**
	 * 正则到字符串
	 * @param str
	 * @return
	 */
	public static String conversionRegexToStr(String str){
		if(str==null||str.equals(""))
			return str;
		for(Iterator<String> it=REGEX_MAP.keySet().iterator();it.hasNext();){
			String key=it.next();
			str=str.replaceAll(key, REGEX_MAP.get(key));
		}
		return str;
	}
	/**
	 * HTML到字符串
	 * @param str
	 * @return
	 */
	public static String conversionHtmlToStr(String str){
		if(str==null||str.equals(""))
			return str;
		for(Iterator<String> it=HTML_MAP.keySet().iterator();it.hasNext();){
			String key=it.next();
			str=str.replaceAll(key, HTML_MAP.get(key));
		}
		return str;
	}
	/**
	 * 字符串到正则
	 * @param str
	 * @return
	 */
	public static String conversionStrToRegex(String str){
		if(str==null||str.equals(""))
			return str;
		for(Iterator<String> it=REGEX_MAP.keySet().iterator();it.hasNext();){
			String key=it.next();
			str=str.replaceAll(REGEX_MAP.get(key),key);
		}
		return str;
	}
	/**
	 * 字符串到HTML
	 * @param str
	 * @return
	 */
	public static String conversionStrToHtml(String str){
		if(str==null||str.equals(""))
			return str;
		for(Iterator<String> it=HTML_MAP.keySet().iterator();it.hasNext();){
			String key=it.next();
			str=str.replaceAll(HTML_MAP.get(key),key);
		}
		return str;
	}

}
